package com.cjbdi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjbdi.entity.Message;
import com.cjbdi.pagination.PageInfo;
import com.cjbdi.service.MessageService;
import com.cjbdi.util.NumUtil;
import com.cjbdi.vo.Result;
import com.cjbdi.vo.UserGonggaoVO;
import com.cjbdi.websocket.WebSocketServer;

@Controller
@RequestMapping("message")
public class MessageController {
	@Autowired
    private MessageService messageService;
    
	@CrossOrigin
	@RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result save(Message message) {
    		message.setCreated(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    		messageService.save(message);
        	return new Result(200,"发送留言成功",null);
    	
    }

    
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> page(@RequestParam(value = "limit", required = false, defaultValue = "10") String limit,
			@RequestParam(value = "offset", required = false, defaultValue = "0") String offset,Message message) {
    	Sort sort = new Sort(Direction.DESC,"id");
    	Pageable pageable = PageRequest.of(Integer.valueOf(offset)/Integer.valueOf(limit), Integer.valueOf(limit), sort);
		PageInfo<Message> pageInfo = messageService.getPageByParam(message.getDescription()==null?"":message.getDescription(), message.getFymc()==null?"":message.getFymc(), pageable);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("count", pageInfo.getRecords());
		result.put("queryData", pageInfo.getRows());
		return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(int id){
    	boolean a = messageService.delete(id);
    	return new Result(200,"删除留言成功",null);
    }

}
